// ##############################
// ####  note:  bokeweilai uart main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_log.h"
#include "bkwl_event.h"
#include "bkwl_str.h"
#include "mbedtls/md.h"

bk_err_t bkwl_hmac_sha1(const unsigned char *key, int key_length, const unsigned char *message, int message_length, unsigned char output[20])
{
	bk_err_t ret = BK_OK;
	mbedtls_md_context_t ctx;
	mbedtls_md_init(&ctx);
	const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);

	mbedtls_md_hmac(md_info,
					key, key_length,
					message, message_length,
					output);
	return ret;
}

// 签名
bk_err_t bkwl_get_hmac_data(char *message, char *key, char *out_data)
{
	bk_err_t ret = BK_OK;
	unsigned char output[20];
	bkwl_hmac_sha1((const unsigned char *)key, strlen(key), (const unsigned char *)message, strlen(message), output);
	// tprintf("bkwl_get_hmac_data data:\n");
	// for (int i = 0; i < 20; i++)
	// {
	// 	tprintf("%02x", output[i]);
	// }
	// tprintf("\n");
	bkwl_bytes_to_hex(output, out_data);
	return ret;
}

bk_err_t bkwl_bytes_to_hex(uint8_t *data, char *out_data)
{
	bk_err_t ret = BK_OK;
	char tmp_buf[3] = {0};
	for (int i = 0; i < 20; i++)
	{
		sprintf(tmp_buf, "%02x", data[i]);
		strcat((char *)out_data, tmp_buf);
	}

	return ret;
}